package com.example.flowermall.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flowermall.entity.Product;
import com.example.flowermall.entity.ProductImg;
import com.example.flowermall.entity.Result;
import com.example.flowermall.mapper.ProductMapper;
import com.example.flowermall.service.IProductImgService;
import com.example.flowermall.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-06-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    IProductImgService productImgService;

    @Override
    public Result insertGoods(Product product) {
        Integer categoryId = productMapper.selectCategoryId(product.getCategoryName());
        List<ProductImg> productImgList = product.getProductImgList();
        product.setCategoryId(categoryId);
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        List<Product> productList = productMapper.selectList(queryWrapper);
        List<String> name = productList.stream().map(o -> o.getProductName()).collect(Collectors.toList());
        if(name.contains(product.getProductName())){
            return Result.error("商品已经存在");
        }
        productMapper.insert(product);
        queryWrapper.eq(Product::getProductName,product.getProductName());
        Product product1 = productMapper.selectOne(queryWrapper);
        if(productImgList!=null){
            for (int i=0;i<productImgList.size();i++){
                ProductImg productImg = new ProductImg();
                if(Objects.equals(product.getProductUrl(), productImgList.get(i).getImgUrl())){
                    productImg.setMainImg(1);
                }
                productImg.setProductId(product1.getProductId());
                productImg.setImgUrl(productImgList.get(i).getImgUrl());
                productImg.setCreateTime(LocalDateTime.now());
                productImg.setIsIntegral(0);
                productImgService.add(productImg);
            }
        }
        return Result.success("创建成功");
    }

    @Override
    public Product getGoodsById(Integer id) {
        Product product = productMapper.selectById(id);
        if(ObjectUtils.isEmpty(product)){
            return product;
        }
        ProductImg byProductId = productImgService.getByProductId(product.getProductId());
        if(byProductId!=null){
            product.setProductUrl(byProductId.getImgUrl());
        }
        return product;
    }

    @Override
    public void updateGoods(Product product) {
        //List<ProductImg> listByProductId = productImgService.getListByProductId(product.getProductId());
        productImgService.deleteByProductId(product.getProductId());
        List<ProductImg> productImgList = product.getProductImgList();
        /*for(int i=0;i<listByProductId.size();i++){
            for (int j=0;j<productImgList.size();j++){
                if(Objects.equals(productImgList.get(j).getImgUrl(), listByProductId.get(i).getImgUrl())){
                    productImgList.remove(j);
                }
            }
        }*/
        if(productImgList!=null){
            for(int i=0;i<productImgList.size();i++){
                ProductImg productImg = new ProductImg();
                if(Objects.equals(product.getProductUrl(), productImgList.get(i).getImgUrl())){
                    productImg.setMainImg(1);
                }
                productImg.setProductId(product.getProductId());
                productImg.setImgUrl(productImgList.get(i).getImgUrl());
                productImg.setCreateTime(LocalDateTime.now());
                productImg.setIsIntegral(0);
                productImgService.add(productImg);
            }
        }
        Integer categoryId = productMapper.selectCategoryId(product.getCategoryName());
        product.setCategoryId(categoryId);
        productMapper.updateById(product);
    }

    @Override
    public void deleteGoods(Integer id) {
        productMapper.deleteById(id);
    }

    @Override
    public List<Product> selectGoods(String productName) {
        return productMapper.selectGoods(productName);

    }

    @Override
    public String selectCategory(Integer productId) {
        return productMapper.selectCategory(productId);
    }

    @Override
    public List<Product> selectGoodsByCategoryId(Integer id) {
        List<Product> products = productMapper.selectGoodsByCategoryId(id);
        for (Product product : products) {
            ProductImg productImg = productImgService.getByProductId(product.getProductId());
            if (productImg != null) {
                product.setProductUrl(productImg.getImgUrl());
            }

        }
        return products;
    }

    @Override
    public List<Product> selectByTagId(Integer id) {
        List<Product> productList = productMapper.selectByTagId(id);
        for (Product product : productList) {
            ProductImg productImg = productImgService.getByProductId(product.getProductId());
            if (productImg != null) {
                product.setProductUrl(productImg.getImgUrl());
            }

        }
        return productList;
    }

    @Override
    public Product getById(Integer id) {
        Product product = productMapper.selectById(id);
        List<ProductImg> listByProductId = productImgService.getListByProductId(id);
        if(listByProductId!=null){
            product.setProductImgList(listByProductId);
        }

        return product;
    }

    @Override
    public List<Product> getAllCategory() {
        LambdaQueryWrapper<Product> queryWrapper=new LambdaQueryWrapper<>();
        return productMapper.selectList(queryWrapper);
    }


}
